﻿using FastReport;
using FastReport.Data;
using FastReport.Utils;
using SMALLERP.DataClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XJLSC.PM
{
    public partial class LabelPrint : Form
    {
        DataBase db;
        private SqlDataReader sdr1;
        private Report FReport;
        private DataSet FDataSet;
        private DataSet FDataSet1;
        public delegate void Cdssh(object sender, EventArgs e);
        //定义委托对象
        public Cdssh cdssh;
        public LabelPrint()
        {
            InitializeComponent();
        }

        private void LabelPrint_Load(object sender, EventArgs e)
        {
            string strSql = null;
            string strSql1 = null;
            string code = null;
            string GZZX = "";
            strSql = "SELECT TOP 1 TA021 FROM SFCTE LEFT JOIN MOCTA ON TA001=TE006 AND TA002=TE007 WHERE TE001='" + SJCD.SSH + "' AND TE002='" + SJCD.DYMB + "' AND TA021<>''";

            try
            {
                db = new DataBase();
                sdr1 = db.GetDataReader1(strSql);

                while (sdr1.Read())
                {
                    GZZX = sdr1["TA021"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            finally
            {
                sdr1.Close();
            }

            FDataSet = new DataSet();
            FReport = new Report();
            FReport.Preview = preview1;
            string reportName = "";
            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\");
            if(GZZX=="1008" || GZZX == "1007")
            {
                reportName = theFolder + "派工卡.frx";
            }
            else
            {
                reportName = theFolder + "派工卡1.frx";
            }
            FReport.Load(reportName);
            db = new DataBase();

                 strSql = "SELECT ISNULL(TY003,TE023) 要求完工日期,TE001+'-'+TE002+'-'+TE003 PGDH,ISNULL(MB002,'') 部件名称,ISNULL(MF023,'')+TE009 AS TE009,SFCTE.*,SFCTD.*,TA015,TA083,MV002,TA001,TA002,MB029 FROM SFCTE LEFT JOIN SFCTD ON TE001=TD001 AND TE002=TD002 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN MOCTA ON TE006=TA001 AND TE007=TA002 LEFT JOIN BOMMF ON MF001=TE017 AND MF003=TE008 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN MOCTY ON TY001=TE006 AND TY002=TE007 AND TY019=(SELECT TOP 1 A.MF004 FROM BOMMF A WHERE A.MF001=TE017 AND A.MF003>TE008 AND NOT EXISTS(SELECT * FROM BOMMF B WHERE A.MF001=B.MF001 AND MF002=B.MF002 AND B.MF003>TE008 AND B.MF003>A.MF003)) WHERE TE001='" + SJCD.SSH + "' AND TE002='" + SJCD.DYMB + "' ORDER BY TE003";     
                 strSql1 = "SELECT TB003 品号,TB012 品名,TB013 规格,CAST(TB004 AS REAL) 需领数量,CAST(TB005 AS REAL) 已领数量,CAST(TB004-TB005 AS REAL) 未领数量,ISNULL(MB002,'') 部件名称,ISNULL(MF023,'')+TE009 AS TE009 FROM SFCTE  LEFT JOIN MOCTB ON TE006=TB001 AND TE007=TB002 AND TE009=TB006 LEFT JOIN BOMMF ON MF001=TE017 AND MF003=TE008 LEFT JOIN INVMB ON MF023=MB001 WHERE TE001='" + SJCD.SSH + "' AND TE002='" + SJCD.DYMB + "' ORDER BY TE003";
                  FDataSet = db.GetDataSet3(strSql, "Table",strSql1, "Table1");
                  FReport.RegisterData(FDataSet.Tables[0], "Table");
                  FReport.RegisterData(FDataSet.Tables[1], "Table1");
            //给DataBand(主表数据)绑定数据源
            DataBand masterBand = FReport.FindObject("Data1") as DataBand;
            masterBand.DataSource = FReport.GetDataSource("Table"); //主表

            ////给DataBand(明细数据)绑定数据源
            DataBand detailBand = FReport.FindObject("Data2") as DataBand;
            detailBand.DataSource = FReport.GetDataSource("Table1"); //明细表

            //重要！！给明细表设置主外键关系！
            //DataBand masterBand = FReport.FindObject("Data1") as DataBand;
            //DataBand detailBand = FReport.FindObject("Data2") as DataBand;
            detailBand.Relation = new Relation();
            detailBand.Relation.ParentColumns = new string[] { "TE009" };
            detailBand.Relation.ParentDataSource = FReport.GetDataSource("Table"); //主表
            detailBand.Relation.ChildColumns = new string[] { "TE009" };
            detailBand.Relation.ChildDataSource = FReport.GetDataSource("Table1"); //明细表
            FReport.Prepare();
            FReport.ShowPrepared();         
        }
    }
}
